package com.snailhd.config;

import com.alibaba.fastjson.JSON;
import com.snailhd.entity.MqttMsg;
import com.snailhd.handler.MqttMsgExecutor;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttMessage;

@Slf4j
public class MqttRecieveCallback implements MqttCallback {

    @Override
    public void connectionLost(Throwable cause) {

    }

    @Override
    public void messageArrived(String topic, MqttMessage message) throws Exception {
        String msg = new String(message.getPayload());
        log.info("接收消息-->topic:{}，Qos:{}，msg:{}",topic,message.getQos(),new String(message.getPayload()));
        MqttMsg mqttMsg = JSON.parseObject(msg, MqttMsg.class);
        MqttMsgExecutor.exec(mqttMsg);
    }

    @Override
    public void deliveryComplete(IMqttDeliveryToken token) {

    }

}
